package com.corpgovernment.organization.dataloader.db;

import com.corpgovernment.organization.model.SupplierCompanyModel;
import com.corpgovernment.supplier.entity.db.SupplierCompany;
import com.corpgovernment.supplier.mapper.SupplierCompanyMapper;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zl.cui
 * @date 2023/10/12
 */
@Component
@Slf4j
public class MbSupplierCompanyLoader {
    @Autowired
    private SupplierCompanyMapper supplierCompanyMapper;

    public List<SupplierCompanyModel> getSupplierCompanyConfig(String orgId, String supplierCode){
        List<SupplierCompany> mbSupplierCompanyList = supplierCompanyMapper.selectProductBySupplierCodeAndCompany(supplierCode, orgId);
        if (CollectionUtils.isEmpty(mbSupplierCompanyList)){
            return Collections.emptyList();
        }
        log.info("getSupplierCompanyConfig:{}", JsonUtils.toJsonString(mbSupplierCompanyList));
        return mbSupplierCompanyList.stream()
                .map(this::convert)
                .collect(Collectors.toList());

    }
    private SupplierCompanyModel convert(SupplierCompany mbSupplierCompany){
        SupplierCompanyModel model = new SupplierCompanyModel();
        BeanUtils.copyProperties(mbSupplierCompany, model);
        log.info("convert:{}", JsonUtils.toJsonString(model));
        return model;
    }

}
